package com.xs.shop.goods.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xs.shop.goods.model.entity.GoodsImage;
import com.xs.shop.goods.service.mapper.GoodsImageMapper;
import com.xs.shop.goods.service.service.GoodsImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xs
 * @since 2025-07-16
 */
@Service
public class GoodsImageServiceImpl extends ServiceImpl<GoodsImageMapper, GoodsImage> implements GoodsImageService {
    @Override
    public String getMainByGoodsId(Long goodsId) {
        LambdaQueryWrapper<GoodsImage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsImage::getGoodsId, goodsId);
        wrapper.eq(GoodsImage::getIsMain, 1);

        GoodsImage goodsImage = this.getOne(wrapper);
        return goodsImage.getImageUrl();
    }

    @Override
    public List<String> getImagesByGoodsId(Long goodsId) {
        List<String> images=new ArrayList<>();
        // 图片列表 = 主图 + 其他的非主图部分
        LambdaQueryWrapper<GoodsImage> wrapperMain = new LambdaQueryWrapper<>();
        wrapperMain.eq(GoodsImage::getGoodsId, goodsId);
        wrapperMain.eq(GoodsImage::getIsMain, 1);
        //主图
        GoodsImage mainImage = this.getOne(wrapperMain);
        //第一位加入主图
        images.add(mainImage.getImageUrl());
        //非主图
        LambdaQueryWrapper<GoodsImage> wrapperSlave = new LambdaQueryWrapper<>();
        wrapperSlave.eq(GoodsImage::getGoodsId, goodsId);
        wrapperSlave.eq(GoodsImage::getIsMain, 0);
        List<GoodsImage> slaveImage = this.list(wrapperSlave);
        for (GoodsImage image : slaveImage) {
            images.add(image.getImageUrl());
        }

        return images;
    }
}
